import cv2
import datetime
import time

url = "udpsrc address=0.0.0.0 port=5000 ! application/x-rtp, media=video, encoding-name=H264 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! video/x-raw,width=1280,height=720,format=BGR ! appsink"
cap = cv2.VideoCapture(url, cv2.CAP_GSTREAMER)

last_start_time = 0
first_time = 0

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret:
        start_time = time.time()
        timestamp = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S.%f")[:-3]
        cv2.putText(frame, timestamp, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2)
        cv2.imshow("Input via Gstreamer", frame)
        end_time = time.time()

        process_time = end_time - start_time
        interval_time = start_time - last_start_time
        last_start_time = start_time
        fps = 1 / interval_time
        print("process:", f"{process_time:.5f}", "interval:", f"{interval_time:.3f}", "fps:", f"{fps:.1f}")
        
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()